//无状态组件
import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:personinfo/states/appController.dart';

import '../../utils/ColorUtil.dart';

class Login extends StatefulWidget{
  const Login({super.key});
  @override
  State<StatefulWidget> createState() => _MyHome();
}

class _MyHome extends State<Login>{
  AppController app = Get.put(AppController());
  String _name = '',_password="";
  final _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      home: Scaffold(
        body: Form(
            key: _formKey,
            child: Container(
              padding: EdgeInsets.all(20.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[


                  TextFormField(
                    decoration:const InputDecoration(
                      icon: Icon(Icons.people),
                      labelText: "用户名或手机号",
                      hintText:  "请输入用户名或手机号",
                    ),
                    validator: (val){
                      if(val==null || val == ''){
                        return "请输入用户名或手机号";
                      }
                      return null;
                    },
                    onChanged: (val){
                      setState((){
                        _name = val;
                      });
                    },
                  ),

                  TextFormField(
                    obscureText: true,
                    decoration: const InputDecoration(
                      icon: Icon(Icons.lock), labelText: "密码",
                      hintText:  "请输入密码",
                    ),
                    validator: (val){
                      if(val==null || val == ''){
                        return "请输入密码";
                      }
                      return null;
                    },
                    onChanged: (val){
                      setState(() {
                        _password = val;
                      });
                    },
                  ),
                  const SizedBox(
                    height: 30,
                  ),

                  SizedBox(
                    width: double.infinity,
                    height: 44,
                    child: ElevatedButton(
                      style: ElevatedButton.styleFrom(
                        backgroundColor: ColorUtil.primaryBgColor
                      ),
                      child: const Text(
                        "登录",
                        style: TextStyle(fontSize: 20, color: Colors.white),
                      ),
                      onPressed: () {
                        if(_formKey.currentState!.validate()){
                          app.login(_password,name: _name,id: 11, password: _password);
                          // Navigator.pushNamed(context, "/");
                          Navigator.pushReplacementNamed(context, "/");
                        }
                      },
                    ),
                  )


                ],
              ),
            )
        ),
      ),
    );
  }
}


